
#include <rtthread.h>
#include <rtdevice.h>
#include <board.h>
#include <rtdbg.h>
#include "appconfig.h"

#define perss_key 0
#define sw1_key 1
#define sw2_key 2
#define sw3_key 3
#define sw4_key 4
#define sw5_key 5
#define sw6_key 6


static rt_event_t key_event;
#define key_presson 1<<0
static rt_uint8_t keyVal = 0xff;

int appkey_env_init(void)
{
    key_event = rt_event_create("key_event",RT_IPC_FLAG_FIFO);
    return 0;
}
INIT_ENV_EXPORT(appkey_env_init);


void get_key_val(rt_uint8_t keyval)
{
    keyVal = keyval;
    rt_event_send(key_event, key_presson);
}

void key_thread_entry(void* param)
{
    while(1)
    {
        if(rt_event_recv(key_event,key_presson,RT_EVENT_FLAG_OR|RT_EVENT_FLAG_CLEAR,RT_WAITING_FOREVER,0)==RT_EOK)
        {
            rt_kprintf("key val: %d\n",keyVal);
            LOG_E("start write sysparams.cfg.PressCali1!\n");
            sysparams.PressCali1 = -runparam.pressVal;
            writeSysParam();
        }

    }
}

int key_thread_init(void)
{
    rt_thread_t tid = rt_thread_create("keyapp", key_thread_entry, RT_NULL, 1024, 11,20);
    if(tid)
        rt_thread_startup(tid);
    else
        rt_kprintf("creat key_thread_entry fail!\n");
    return 0;
}
INIT_APP_EXPORT(key_thread_init);


